<?php
class EditUserForm extends Form
{
	function EditUserForm()
	{
		Form::Form('EditUser');

        $this->link_js('javascript/jquery/packed/jquery.validate.js');
        $this->link_js('javascript/jquery/packed/jquery.jeditable.js');
        $this->link_js("javascript/useradmin.js");
        $this->link_js('javascript/rounded-corners.js');
        $this->link_js('javascript/form-field-tooltip.js');
        $this->link_css('style/form-field-tooltip.css');

        $this->link_css("style/paging.css");
        $this->link_css('style/jquery-ui-1.9.2.custom.css');
        $this->link_css('style/msgBoxLight.css');
        $this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
        //
        $this->link_js("javascript/jquery/jquery.ui.core.js");
        $this->link_js("javascript/jquery/jquery.ui.widget.js");
        $this->link_js("javascript/jquery/jquery.ui.mouse.js");
        $this->link_js("javascript/jquery/jquery.ui.sortable.js");
        $this->link_js("javascript/jquery/jquery.ui.datepicker.js");
        $this->link_js("javascript/jquery/jquery.msgBox.js");
		
		CGlobal::$website_title = "Thay đổi thông tin cá nhân";						
	}

	function on_submit()
	{
		if(!User::is_block()){
			$user_id = (int)Url::get('user_id');
			if(User::is_root() || $user_id == User::id()){//Check đúng user hoặc root mới được cập nhật

				$full_name 	 = trim(Url::get('full_name'));
				$mobile_phone = trim(Url::get('mobile_phone'));
                $birth_day = trim(Url::get('birth_day'));
                $gender = Url::get('gender');
                $address = trim(Url::get('address'));
                $website = trim(Url::get('website'));
                $yahoo_id = trim(Url::get('yahoo_id'));
                $skype_id = trim(Url::get('skype_id'));

                //Phân nhóm cho user
                $gids = '';
                /*foreach (Url::get('gids') as $a){
                    $gids.=$a.'|';
                }*/
                //Upload avatar
                if($_FILES["avatar"]["name"]){
                    $avatar = $_FILES["avatar"]["name"];
                    if($avatar!=''){
                        if($_FILES["avatar"]["tmp_name"]){
                            if(Upload::storage_check_dir("user_ava/", true, IMAGE_SERVER_NO))
                            {
                                $img_ext = explode(".", $avatar);
                                $file_ext = array_pop($img_ext);
                                if(!in_array($file_ext,array('jpg','png','jpeg','gif'))){
                                    $this->showFormErrorMessages("error_file_ext","File tải lên không đúng định dạng!");
                                    return ;
                                }
                                $file_name = UP_FOLDER."/user_ava/" . String::_name_cleaner(Unicode::stripUnicode($full_name),'-').'.'.$file_ext ;
                                $sourceName = $_FILES["avatar"]["tmp_name"];
                                if(!Upload::storage_put_file($file_name, $sourceName, IMAGE_SERVER_NO, FTP_BINARY,1)){
                                    $this->showFormErrorMessages("error_upload_file","Có lỗi xảy ra khi upload file!");
                                    return ;
                                };
                                $avatar_url = $file_name;
                            }
                        }
                    }else{
                        $avatar_url = '';
                    }
                }else{
                    $avatar_url = '';
                }

                if(!isset($_POST['email_subcribe'])){
					$email_subcribe = 0;
				}
				else{
					$email_subcribe = 1;
				}
				
				$new_row=array	(
									'full_name'			=>	$full_name,
									'email_subcribe'	=>	$email_subcribe,
									'mobile_phone'		=>	$mobile_phone,
                                    'birth_day'         =>  $birth_day,
                                    'gender'            =>  $gender,
                                    'address'           =>  $address,
                                    'website'           =>  $website,
                                    'yahoo_id'          =>  $yahoo_id,
                                    'skype_id'          =>  $skype_id,
                                    'gids'             =>   $gids,
								);
                if($avatar_url != ''){
                    $new_row = array_merge($new_row,array('avatar_url' => $avatar_url));
                }
				DB::update('user',$new_row, 'id='.$user_id);
				
				User::getUser( $user_id , 1);
			}
			//Url::redirect_current(array('cmd', 'user_id' => $user_id));
            Url::redirect_url('user.html');
		}
		else{
			$this->setFormError('',"Tài khoản của bạn hiện đang bị khóa, bạn không thể thực hiện chức năng này!");
		}
	}
	
	function draw()
	{
		global $display;
        if(User::is_root()){
            if(Url::get('user_id')){
                $user = DB::select("user","id=".Url::get('user_id'));
            }else{
                $user = DB::select("user","id=".User::id());
            }
        }else{
            $user = DB::select("user","id=".User::id());
        }

        $gids = explode('|', $user['gids']);
        $display->add('gids',$gids);

        /*foreach($user as $key=>$value)
        {
            if(is_string($value) and !isset($_REQUEST[$key]))
            {
                $_REQUEST[$key] = $value;
            }
        }*/
        $groups = User::get_system_groups();
        $display->add('groups',$groups);
        $display->add('msg',$this->showFormErrorMessages(1));
		$display->add('user',$user);
        $display->add('server_img',IMAGE_PATH);
		$display->add('begin_form',$this->beginForm(true,'POST',false,false,1));
		$display->add('end_form',$this->endForm(1));					

		$display->output('edit_user');				
	}
}
?>
